package com.ruoyi.system.service.estate.impl;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.estate.ComplaintMapper;
import com.ruoyi.system.mapper.estate.EstateResidentMapper;
import com.ruoyi.system.domain.estate.Complaint;
import com.ruoyi.system.domain.estate.Resident;
import com.ruoyi.system.service.estate.IComplaintService;

/**
 * 投诉管理Service业务层处理
 */
@Service
public class ComplaintServiceImpl implements IComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;
    
    @Autowired
    private EstateResidentMapper residentMapper;

    /**
     * 查询投诉列表
     * 
     * @param complaint 投诉信息
     * @return 投诉集合
     */
    @Override
    public List<Complaint> selectComplaintList(Complaint complaint) {
        return complaintMapper.selectComplaintList(complaint);
    }

    /**
     * 查询投诉详细信息
     * 
     * @param complaintId 投诉ID
     * @return 投诉信息
     */
    @Override
    public Complaint selectComplaintById(Long complaintId) {
        return complaintMapper.selectComplaintById(complaintId);
    }

    /**
     * 新增投诉
     * 
     * @param complaint 投诉信息
     * @return 结果
     */
    @Override
    public int insertComplaint(Complaint complaint) {
        // 生成投诉编号
        if (StringUtils.isEmpty(complaint.getComplaintCode())) {
            String complaintCode = generateComplaintCode();
            complaint.setComplaintCode(complaintCode);
        }
        
        // 设置投诉人ID，如果前端没有传入，则使用当前登录用户关联的住户ID
        if (complaint.getResidentId() == null) {
            Long userId = SecurityUtils.getUserId();
            Resident resident = residentMapper.selectResidentByUserId(userId);
            if (resident == null) {
                throw new ServiceException("您尚未关联住户信息，请先完善住户资料或联系管理员");
            }
            complaint.setResidentId(resident.getResidentId());
            // 确保设置正确的住户名称，而不是用户账号
            complaint.setResidentName(resident.getResidentName());
            complaint.setContactPhone(resident.getPhone());
        } else {
            // 如果前端传入了住户ID但没有传入住户名称，则查询住户名称
            if (StringUtils.isEmpty(complaint.getResidentName())) {
                Resident resident = residentMapper.selectResidentById(complaint.getResidentId());
                if (resident != null) {
                    complaint.setResidentName(resident.getResidentName());
                    if (StringUtils.isEmpty(complaint.getContactPhone())) {
                        complaint.setContactPhone(resident.getPhone());
                    }
                }
            }
        }
        
        // 设置基本字段
        complaint.setCreateTime(new Date());
        complaint.setCreateBy(SecurityUtils.getUsername());
        complaint.setComplaintTime(new Date());
        complaint.setComplaintStatus("0");
        complaint.setDelFlag("0");
        complaint.setStatus("0");
        
        // 确保不会覆盖前端传递的字段值
        // 这些字段在前端已经设置，不需要在这里重新设置或覆盖
        // locationText、address、images字段保持原样
        
        return complaintMapper.insertComplaint(complaint);
    }

    /**
     * 生成投诉编号
     * 格式：TS + 年月日 + 4位序列号
     */
    private String generateComplaintCode() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        return "TS" + dateStr + String.format("%04d", System.currentTimeMillis() % 10000);
    }

    /**
     * 修改投诉
     * 
     * @param complaint 投诉信息
     * @return 结果
     */
    @Override
    public int updateComplaint(Complaint complaint) {
        complaint.setUpdateTime(new Date());
        complaint.setUpdateBy(SecurityUtils.getUsername());
        return complaintMapper.updateComplaint(complaint);
    }

    /**
     * 处理投诉
     * 
     * @param complaint 投诉信息
     * @return 结果
     */
    @Override
    public int handleComplaint(Complaint complaint) {
        complaint.setUpdateTime(new Date());
        complaint.setUpdateBy(SecurityUtils.getUsername());
        complaint.setHandleTime(new Date());
        complaint.setHandlerId(SecurityUtils.getUserId());
        complaint.setComplaintStatus("2");
        return complaintMapper.updateComplaint(complaint);
    }

    /**
     * 评价投诉处理
     * 
     * @param complaint 投诉信息
     * @return 结果
     */
    @Override
    public int evaluateComplaint(Complaint complaint) {
        complaint.setUpdateTime(new Date());
        complaint.setUpdateBy(SecurityUtils.getUsername());
        complaint.setEvaluationTime(new Date());
        complaint.setComplaintStatus("3");
        return complaintMapper.updateComplaint(complaint);
    }

    /**
     * 撤销投诉
     * 
     * @param complaint 投诉信息
     * @return 结果
     */
    @Override
    public int cancelComplaint(Complaint complaint) {
        complaint.setUpdateTime(new Date());
        complaint.setUpdateBy(SecurityUtils.getUsername());
        complaint.setComplaintStatus("4"); // 4表示已撤销
        return complaintMapper.updateComplaint(complaint);
    }

    /**
     * 批量删除投诉
     * 
     * @param complaintIds 需要删除的投诉ID
     * @return 结果
     */
    @Override
    public int deleteComplaintByIds(Long[] complaintIds) {
        return complaintMapper.deleteComplaintByIds(complaintIds);
    }

    /**
     * 删除投诉信息
     * 
     * @param complaintId 投诉ID
     * @return 结果
     */
    @Override
    public int deleteComplaintById(Long complaintId) {
        return complaintMapper.deleteComplaintById(complaintId);
    }
} 